package com.ifnk.portaljava.hlyt.hsqldb;

import com.ifnk.portaljava.hlyt.dao.IBehaviourJdbcDAO;
import com.ifnk.portaljava.hlyt.service.ILogService;

import java.sql.Connection;
import java.sql.SQLException;

public class HSQLServiceImpl implements HSQLService{
	private IBehaviourJdbcDAO behaviourJdbcDao;
	private ILogService logService;
	private HsqlDao hsqlDao;
	public IBehaviourJdbcDAO getBehaviourJdbcDao() {
		return behaviourJdbcDao;
	}

	public void setBehaviourJdbcDao(IBehaviourJdbcDAO behaviourJdbcDao) {
		this.behaviourJdbcDao = behaviourJdbcDao;
	}

	public ILogService getLogService() {
		return logService;
	}

	public void setLogService(ILogService logService) {
		this.logService = logService;
	}

	public HsqlDao getHsqlDao() {
		return hsqlDao;
	}

	public void setHsqlDao(HsqlDao hsqlDao) {
		this.hsqlDao = hsqlDao;
	}

	public void add(String sql) {
		Connection con = null;
		try {
			con = hsqlDao.getConnection();
			con.setAutoCommit(false);
			hsqlDao.save(sql, con);
			con.commit();
		}catch(Exception e) {
			try {
				if(con != null)
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
	}

	public void delete() {
		Connection con = null;
		try {
			con = hsqlDao.getConnection();
			con.setAutoCommit(false);
			hsqlDao.delete(con);
			con.commit();
		}catch(Exception e) {
			try {
				if(con != null)
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally {
			try {
				if(con != null)
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public void find() {
		try {
			hsqlDao.find();
		}catch(Exception e) {
			e.printStackTrace();
		}
	}

	public void drop() {
		try {
			hsqlDao.dropTable();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void update() {
		Connection con = null;
		try {
			con = hsqlDao.getConnection();
			con.setAutoCommit(false);
			hsqlDao.update(con);
			con.commit();
		}catch(Exception e) {
			try {
				if(con != null)
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally {
			try {
				if(con != null)
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	
	
}
